package org.gitman.agentplatform.ui.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;

import org.gitman.agentplatform.ui.api.View;
import org.gitman.agentplatform.ui.api.View.MessageType;

public class SaveGraphAction extends UIAction{

	public SaveGraphAction(){
		super("Save Graph");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(graphEditor.getGraph() == null){
			View.showMessage("No graph to be saved", MessageType.ERROR);
		}else{
			JFileChooser fc = new JFileChooser(System.getProperty("installDir") + "/graphs");
			int rc = fc.showSaveDialog(mainFrame);
			if(rc == JFileChooser.APPROVE_OPTION){
				File f = fc.getSelectedFile();
				try{
					if(!f.exists() && !f.getName().endsWith(".png")){
						f = new File(f.getAbsoluteFile() + ".png");
					}
					mainFrame.save(f);
				}catch (IOException ex) {
					View.showMessage(ex.getMessage(), MessageType.ERROR);
					ex.printStackTrace();
				}
				
			}
		}
	}

}
